const { resolve, dirname } = require('path')
const { CleanWebpackPlugin } = require('clean-webpack-plugin')
const HtmlWebpackPlugin = require('html-webpack-plugin')
const NODE_ENV = process.env.NODE_ENV === 'development' ? 'development' : 'production'
module.exports = {
  mode: NODE_ENV,
  entry: {
    app: './src/main.ts'
  },
  output: {
    path: resolve('./dist'),
    filename: '[name].[contenthash:8].js' // 以入口文件为name.8位hash值命名
  },
  module: {
    rules: [{
      test: /\.tsx?$/,
      use: ['ts-loader'],
      include: [resolve('./src')]
    }],
  },
  plugins: [
    new CleanWebpackPlugin(),
    new HtmlWebpackPlugin({
      template: './public/index.html'
    })
  ],
  resolve: {
    extensions: ['.ts', '.js', '.tsx'] // 引入文件不用写扩展名
  },
  devtool: NODE_ENV === 'development' ? 'true' : 'false',
  devServer: {
    port: 9527,
    open: true,
    host: 'localhost',
    stats: 'errors-only'
  }
}